package com.hk.dao.impl;

import com.hk.dao.IContactUsDao;
import com.hk.model.ContactUs;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: zhe
 * @Date: 2021/6/15 11:37
 * @Statement: 联系我们数据持久层
 */
public class ContactUsDaoImpl implements IContactUsDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";


    @Override
    public ResultSet findAll(Integer page, Integer limit, ContactUs t) {
        sql = "select * from contact_us where 1=1 ";
        if (!t.getSite().equals("")) {
            sql += " and site like " + SqlSymbol.MARK.getValue() + SqlSymbol.PERCENT.getValue() + t.getSite() + SqlSymbol.PERCENT.getValue() + SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, ContactUs t) {
        sql = "select count(1) from contact_us where 1=1 ";
        if (!t.getSite().equals("")) {
            sql += " and site like " + SqlSymbol.MARK.getValue() + SqlSymbol.PERCENT.getValue() + t.getSite() + SqlSymbol.PERCENT.getValue() + SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from contact_us where id = " + id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(ContactUs t) {
        sql = "insert into contact_us value " +
                "(" + null + SqlSymbol.COMMA.getValue() + SqlSymbol.MARK.getValue() + t.getSite() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getPhone() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getFax() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getEmail() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getUrl() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                t.getState() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getGmtCreate() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                SqlSymbol.MARK.getValue() + t.getGmtModified() + SqlSymbol.MARK.getValue() +
                SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(ContactUs t) {
        sql = "update contact_us set " +
                "site = " + SqlSymbol.MARK.getValue() + t.getSite() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                "phone = " + SqlSymbol.MARK.getValue() + t.getPhone() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                "fax = " + SqlSymbol.MARK.getValue() + t.getFax() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                "email = " + SqlSymbol.MARK.getValue() + t.getEmail() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                "url = " + SqlSymbol.MARK.getValue() + t.getUrl() + SqlSymbol.MARK.getValue() + SqlSymbol.COMMA.getValue() +
                "gmt_modified = " + SqlSymbol.MARK.getValue() + t.getGmtModified() + SqlSymbol.MARK.getValue() +
                " where id = " + t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    /**
     * 事务
     * 1、在修改状态前，将所有状态修改为未启用1
     * 2、将当前Id对应联系我们信息状态修改为启用0
     *
     * @param t
     * @return
     */
    @Override
    public int updateState(ContactUs t) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = jdbcMysql.getConnection();
            connection.setAutoCommit(false); //关闭事务自动提交
            statement = connection.createStatement();
            sql = "update contact_us set " +
                    "state = " + 1 + SqlSymbol.COMMA.getValue() +
                    "gmt_modified = " + SqlSymbol.MARK.getValue() + t.getGmtModified() + SqlSymbol.MARK.getValue();
            int i = statement.executeUpdate(sql);
            sql = "update contact_us set " +
                    "state = " + t.getState() + SqlSymbol.COMMA.getValue() +
                    "gmt_modified = " + SqlSymbol.MARK.getValue() + t.getGmtModified() + SqlSymbol.MARK.getValue() +
                    " where id = " + t.getId();
            int i1 = statement.executeUpdate(sql);
            if (i < 1 || (i1 < 1 || i1 >1)) {
                System.out.println("修改状态操作未成功！");
                connection.rollback();//数据回滚
                return 0;
            }
            connection.commit(); //事务提交
            return i1;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally { //关闭资源
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        sql = "delete from contact_us where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }
}
